import json
import datetime

class JsonResult:
    def __init__(self):
        pass

    # 定义一个replacer函数来处理日期对象
    def date_serializer(self,obj):
        if isinstance(obj, datetime.date):
            return obj.isoformat()
        raise TypeError("Object of type '%s' is not JSON serializable" % type(obj).__name__)

    def setOk(self,msg,data=[],num=0):
        rs = {
            "code":0,
            "message": msg,
            "count": len(data) if type(data) == list or type(data) == dict else 0,
            "num": num,
            "data": data
        }
        return json.dumps(rs, ensure_ascii=False, default=self.date_serializer).encode('utf8')

    def setError(self,msg):
        rs = {
            "code": -1,
            "message": msg,
            "count": 0,
            "data": []
        }
        return json.dumps(rs)